(function() {
	/*
	 * 请求购物车数据
	 */
	function _cart_get_cart_success(result) {
		var cart_detail = result.result;
		App.set("user.cart_detail", cart_detail);
		App.set("$Cache.cart.total_price", Model.Observer(function() {
			var cart_detail = App.get("user.cart_detail");
			var total_price = 0;
			cart_detail.forEach(function(cart) {
				cart.goods_info.forEach(function(goods_info) {
					total_price += goods_info.goods.price * goods_info.num;
				});
			});
			return total_price;
		}));

		var cart_id_list = jSouper.map(cart_detail, function(cart) {
			return cart._id;
		});
		if (cart_id_list.length) {
			//计算订单价格
			coAjax.post(appConfig.user.cart_get_cash, {
				//user_address_id
				cart_id_list: cart_id_list,
				recommender_id: App.get("recommender_id"),
				guide_id: App.get("guide_id")
			}, function(result) {
				var moni_ach = result.result;
				App.set("$Cache.cart.money_ach", moni_ach);
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
			});
		};
	};

	function _cart_get_cart_error(errorCode, xhr, errorMsg) {
		console.log("%c" + errorCode + ": " + errorMsg, "color:orange;font-size:14px; text-shadow: 0 1px 0 #ccc;");
		alert("error", errorMsg);
	};
	require(["Cookies"], function(Cookies) {
		Path.on("/__basename__.html", function(_current_location) {
			if (!App.get("loginer")) {
				alert("warn", "您未登陆或已退出登陆！！！");
				Path.jump("/main.html");
				return;
			};
			var queryString = _current_location.query;
			var goods_id = queryString.get("goods_id");
			var recommender_id = queryString.get("rcid") || Cookies.get("recommender_id");
			App.set("recommender_id", recommender_id);
			var guide_id = queryString.get("gi") || Cookies.get("guide_id");
			App.set("guide_id", guide_id);
			if (goods_id) {
				coAjax.post(appConfig.user.cart, {
					goods_id: goods_id,
					num: queryString.get("num")
				}, _cart_get_cart_success, _cart_get_cart_error);
			} else {
				coAjax.get(appConfig.user.get_cart_detail_by_bus_id, {
					bus_id: App.get("bus_info.bus_id")
				}, _cart_get_cart_success, _cart_get_cart_error);
			};
		});
	});

	App.set("$Event.cart.cart_to_ach", function(e) {
		var cart_detail = App.get("user.cart_detail");
		var cart_id_list = jSouper.map(cart_detail, function(cart) {
			return cart._id;
		});

		coAjax.post(appConfig.user.cart_make_ach, {
			cart_id_list: cart_id_list,
			recommender_id: App.get("recommender_id"),
			guide_id: App.get("guide_id")
		}, function(result) {
			console.log(result);
			Path.jump("/pay.html?ach_id=" + result.result._id)
		}, function(errorCode, xhr, errorMsg) {
			alert("error", errorMsg);
		});
	});
	App.set("$Event.cart.remove_goods", function(e, vm) {
		coAjax["delete"](appConfig.user.remove_goods_from_cart, {
			goods_id: vm.get("goods_id"),
			cart_id: vm.get("$Parent.$Parent._id")
		}, _cart_get_cart_success, _cart_get_cart_error)
	});
	App.set("$Event.cart.update_cart_num", function(e, vm) {
		coAjax.put(appConfig.user.update_goods_num_in_cart, {
			goods_id: vm.get("goods_id"),
			cart_id: vm.get("$Parent.$Parent._id"),
			num: vm.get("num")
		}, _cart_get_cart_success, _cart_get_cart_error)
	});
}());